﻿using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Common;
using QldtSdh.GUI.Utils;
using QldtSdh.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Windows.Forms;

namespace QldtSdh.GUI.NCS
{
    public partial class FormSendMailToNCS : QldtSdh.GUI.Common.BaseDialog
    {
        private List<NghienCuuSinh> m_LstNguoiNhan;

        public FormSendMailToNCS(List<NghienCuuSinh> lstNCS)
        {
            InitializeComponent();
            m_LstNguoiNhan = lstNCS;
            var stMails = "";
            foreach (var item in m_LstNguoiNhan)
                if (!string.IsNullOrEmpty(item.Email))
                    if (string.IsNullOrEmpty(stMails))
                        stMails = string.Format("{0} <{1}>; ", item.HoTen, item.Email);
                    else
                        stMails += string.Format("; {0} <{1}>", item.HoTen, item.Email);
            txtNguoiNhan.Text = stMails;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            string lsEmail = "";
            string lsID = "";
            var lstNguoiNhan = new List<string>();
            foreach (var item in m_LstNguoiNhan)
                if (!string.IsNullOrEmpty(item.Email))
                {
                    lstNguoiNhan.Add(item.Email);
                    lsEmail += "\r\n" + item.HoTen + ": " + item.Email;
                    lsID += item.MaNCSGUID + ";";
                }
            var lstAttachments = new List<string>();
            if (System.IO.File.Exists(txtTep1.Text))
                lstAttachments.Add(txtTep1.Text);
            if (System.IO.File.Exists(txtTep2.Text))
                lstAttachments.Add(txtTep2.Text);
            if (System.IO.File.Exists(txtTep3.Text))
                lstAttachments.Add(txtTep3.Text);
            var tieuDe = txtTieuDe.Text.Trim();
            var noiDung = txtNoiDung.Text;

            var fproress = new FormProgress();
            fproress.DoWork += (o, t) =>
            {
                string emailSender, emailPassword, smtpServerAddress;
                int smtpServerPort;
                bool useSSL;
                using (var db = CEMS4UG_DHKHContext.GetContext())
                {
                    emailSender = BizAppConfig.GetEmail(db);
                    emailPassword = BizAppConfig.GetEmailPassword();
                    smtpServerAddress = BizAppConfig.GetEmailSmtpServerAddress();
                    smtpServerPort = BizAppConfig.GetEmailSmtpPort();
                    useSSL = BizAppConfig.GetEmailUseSSL();
                }
                SendMail(tieuDe, noiDung, lstNguoiNhan, lstAttachments, emailSender, emailPassword, smtpServerAddress, smtpServerPort, useSSL);
                ThongBaoNCS thongBao = new ThongBaoNCS { DSNguoiNhan = lsID, NgayThongBao = DateTime.Now, NoiDung = txtNoiDung.Text, TieuDe = txtTieuDe.Text };
                BizThongBaoNCS.Add(thongBao);
            };
            fproress.RunWorkerCompleted += (o, t) =>
            {
                DialogUtils.ShowMessage("Đã gởi email thành công tới: " + lsEmail.ToString());
                this.Close();
            };
            fproress.StartWorking(this);
        }

        void SendMail(string subject, string body, List<string> lstMailAddressToReceive, List<string> lstAttachments,
            string senderEmailAddr, string emailPassword, string smtpAddress = "smtp.gmail.com", int portNumber = 587, bool enableSSL = true)
        {
            //string smtpAddress = "smtp.gmail.com";
            string emailFrom =
                //ConfigurationManager.AppSettings["email"];
                senderEmailAddr;
            string password =
                //ConfigurationManager.AppSettings["pass"];
                emailPassword;
            using (MailMessage mail = new MailMessage())
            {
                mail.From = new MailAddress(emailFrom);
                foreach (var x in lstMailAddressToReceive)
                    mail.To.Add(x);
                mail.Subject = subject;
                mail.Body = body;
                mail.IsBodyHtml = true;

                foreach (var x in lstAttachments)
                    mail.Attachments.Add(new Attachment(x));

                using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
                {
                    smtp.Credentials = new NetworkCredential(emailFrom, password);
                    smtp.EnableSsl = enableSSL;
                    smtp.Send(mail);
                }
            }
        }

        private void lnkTep1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var dlg = new OpenFileDialog();
            dlg.Title = "Chọn tệp đính kèm thư...";
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                var ctl = txtTep1;
                if (sender == lnkTep2)
                    ctl = txtTep2;
                else if (sender == lnkTep3)
                    ctl = txtTep3;
                ctl.Text = dlg.FileName;
            }
        }
    }
}
